# load("xxx.RData")



### HELPER FUNCTION

# Duplicate counter (also sorts):

library(data.table)

count.dups <- function(DF){
  DT <- data.table(DF)
  DT[,.N, keyby = names(DT)]
} # from stackoverflow, modified to sort ('keyby')



### TABLE OF TYPES

# A) Tables of Types with l Papers

# Types with zero paper:

temp <- which(exper_auth & lengths(X_list) == 0)

temp0 <- cbind(Z[temp])

temp <- count.dups(temp0)

types0 <- temp[,1, with=FALSE]
counts0 <- temp$N

# Add isolated types absent from sample:

test <- 1:nlevels(Z)

temp <- test[!(test %in% types0$V1) ]

types0 <- c(types0$V1, temp)
counts0 <- c(counts0, rep(0, length(temp) ) )

temp <- order(types0)

types0 <- types0[temp]
counts0 <- counts0[temp]

# Types with one paper:

temp <- which(exper_auth & lengths(X_list) == 1)

temp1 <- matrix(unlist(X_list[temp], use.names=FALSE), byrow=TRUE, ncol=1)
temp1 <- cbind(Z[temp], temp1)

temp <- count.dups(temp1)

types1 <- temp[,1:2, with=FALSE]
counts1 <- temp$N

# Types with two papers:

temp <- which(exper_auth & lengths(X_list) == 2)

temp2 <- matrix(unlist(X_list[temp], use.names=FALSE), byrow=TRUE, ncol=2)
temp2 <- cbind(Z[temp], temp2)

temp <- count.dups(temp2)

types2 <- temp[,1:3, with=FALSE]
counts2 <- temp$N

# Types with three papers:

temp <- which(exper_auth & lengths(X_list) == 3)

temp3 <- matrix(unlist(X_list[temp], use.names=FALSE), byrow=TRUE, ncol=3)
temp3 <- cbind(Z[temp], temp3)

temp <- count.dups(temp3)

types3 <- temp[,1:4, with=FALSE]
counts3 <- temp$N

# Types with four papers:

# (not used, but extra column included for future expansion)

counts4 <- numeric(0)

###

# B) Combine

# All types:

types <- rbind(
	as.matrix( cbind(types0,0,0,0,0) ),
	as.matrix( cbind(types1,  0,0,0) ),
	as.matrix( cbind(types2,    0,0) ),
	as.matrix( cbind(types3,      0) ) )

colnames(types) <- c("Z","X1","X2","X3","X4")

counts <- c(counts0,counts1,counts2,counts3,counts4)

# Number of types:

N_types <- nrow(types)

# Observed type shares:

obs_shares <- counts / N_auth_exp

# Check:

N_auth_exp
sum(counts)

sum(obs_shares)



### CLEAN UP

rm(test, temp, temp0, temp1, temp2, temp3, temp4)
rm(counts0,counts1,counts2,counts3,counts4)
rm(types0,types1,types2,types3,types4)
